package lxt.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import lxt.action.core.ActionSupport;
import lxt.action.core.DateUtil;
import lxt.entity.CostFix;
import lxt.entity.DriverInfo;
import lxt.entity.InvoiceOrder;
import lxt.entity.TruckInfo;
import lxt.entity.UserInfo;
import lxt.service.CostFixService;
import lxt.service.DriverService;
import lxt.service.InvoiceOrderService;
import lxt.service.TruckService;
/**
 * 车辆维修action
 * @author Lit
 *
 */
public class CostFixAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	//定义属性
	private String id;
    private String truckId;
    private String orderId;
    private String driverId;
    private String fixLocation;
    private String fixStation;
    private String fixReason;
    private String totalCharge;
    private String fixDate;
    private String note;
    private Date entryTime;
    private String entryUser;
	//定义service
    private CostFixService costFixService;
    private DriverService driverService ; //用户获取驾驶员
    private TruckService truckService ; //用于获取车辆
    private InvoiceOrderService invoiceOrderService ;//用于获取订单号
    //定义对象
	private List<CostFix> costFixs;
	private List<TruckInfo> trucks; //车牌号list
	private List<InvoiceOrder> invoiceOrders ; //订单号list
	private List<DriverInfo> drivers ; //驾驶员list
	
	private String deleteIds;
	//定义分页变量
	private Integer pageCounts = 10; //每页显示数量
	private String currPageStr = "1"; //分页
	private Integer totalCounts ;//总数据量
    
	/**
	 * 加载
	 * @return
	 */
	public String loadAll(){
		try{
			//定义从第几条显示
			int offset =( Integer.parseInt(currPageStr)-1)*pageCounts;
			//获得交通事故列表
			costFixs = costFixService.loadCostFix(truckId, driverId, fixStation, offset, pageCounts);
			//获取总的记录数
			totalCounts = costFixService.loadAllCostFixCount(truckId, driverId, fixStation);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加预处理
	 * @return
	 */
	public String AddPre(){
		try{
			trucks = truckService.loadAll();
			drivers = driverService.loadAll();
			invoiceOrders = invoiceOrderService.loadAll();
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加
	 * @return
	 */
	public String add(){
		try{
			//创建新的对象
			CostFix costFix = new CostFix();
			//为对象赋值
			costFix.setDriverId(driverId);
			UserInfo user = (UserInfo)this.getSession().get("user");
			costFix.setEntryUser(user.getUsername());
			if(fixDate!=null&&!fixDate.equals("")){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				costFix.setFixDate(sdf.parse(fixDate));
			}
			if(totalCharge!=null&&!totalCharge.equals("")){
				costFix.setTotalCharge(Double.parseDouble(totalCharge));
			}
			costFix.setFixLocation(fixLocation);
			costFix.setFixReason(fixReason);
			costFix.setFixStation(fixStation);
			costFix.setNote(note);
			costFix.setOrderId(orderId);
			costFix.setTruckId(truckId);
			
			
			this.costFixService.addCostFix(costFix);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 编辑预处理
	 * @return
	 */
	public String updatePre(){
		try{
			trucks = truckService.loadAll();
			drivers = driverService.loadAll();
			invoiceOrders = invoiceOrderService.loadAll();
			//获得参数
			CostFix costFix = this.costFixService.loadCostFixById(id);
			UserInfo user = (UserInfo) this.getSession().get("user");
			if(user.getLoginType()!=0){//记录员
				Date preTime = costFix.getEntryTime();
				int falg = DateUtil.getDays(preTime);
				if(falg!=0){
					this.addActionMessage("无权修改数据,请联系管理员");
					this.loadAll();
					return INPUT;
				}
			}
			//为页面属性赋值
			this.driverId = costFix.getDriverId();
			this.fixDate = costFix.getFixDate().toString();
			this.fixLocation = costFix.getFixLocation();
			this.fixReason = costFix.getFixReason();
			this.fixStation = costFix.getFixStation();
			this.id = costFix.getId();
			this.note = costFix.getNote();
			this.orderId = costFix.getOrderId();
			this.totalCharge =String.valueOf(costFix.getTotalCharge()) ;
			this.truckId = costFix.getTruckId();
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 修改
	 * @return
	 */
	public String update(){
		try{
			//重新获取修改目标
			CostFix costFix = this.costFixService.loadCostFixById(id);
			//更新目标对象的目标修改属性(部分属性)
			costFix.setDriverId(driverId);
			if(fixDate!=null&&!fixDate.equals("")){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				costFix.setFixDate(sdf.parse(fixDate));
			}
			if(totalCharge!=null&&!totalCharge.equals("")){
				costFix.setTotalCharge(Double.parseDouble(totalCharge));
			}
			costFix.setFixLocation(fixLocation);
			costFix.setFixReason(fixReason);
			costFix.setFixStation(fixStation);
			costFix.setNote(note);
			costFix.setOrderId(orderId);
			costFix.setTruckId(truckId);
			this.costFixService.updateCostFix(costFix);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
		try{
			/**
			 * 查看用户操作时间 设置编辑权限
			 */
			UserInfo user = (UserInfo) this.getSession().get("user");
			if(user.getLoginType()!=0){//记录员
				for(int i=0;i<deleteIds.length();i++){
					String[] delId = deleteIds.split(",");
					CostFix c = this.costFixService.loadCostFixById(delId[i]);
					int falg = DateUtil.getDays(c.getEntryTime());
					if(falg!=0){
						this.addActionMessage("无权修改数据,请联系管理员");
						this.loadAll();
						return INPUT;
					}
				}
			}
			this.costFixService.deleteCostFix(deleteIds);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * get set
	 * @return
	 */
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTruckId() {
		return truckId;
	}
	public void setTruckId(String truckId) {
		this.truckId = truckId;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public String getDriverId() {
		return driverId;
	}
	public void setDriverId(String driverId) {
		this.driverId = driverId;
	}
	public String getFixLocation() {
		return fixLocation;
	}
	public void setFixLocation(String fixLocation) {
		this.fixLocation = fixLocation;
	}
	public String getFixStation() {
		return fixStation;
	}
	public void setFixStation(String fixStation) {
		this.fixStation = fixStation;
	}
	public String getFixReason() {
		return fixReason;
	}
	public void setFixReason(String fixReason) {
		this.fixReason = fixReason;
	}
	public String getTotalCharge() {
		return totalCharge;
	}
	public void setTotalCharge(String totalCharge) {
		this.totalCharge = totalCharge;
	}
	public String getFixDate() {
		return fixDate;
	}
	public void setFixDate(String fixDate) {
		this.fixDate = fixDate;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public Date getEntryTime() {
		return entryTime;
	}
	public void setEntryTime(Date entryTime) {
		this.entryTime = entryTime;
	}
	public String getEntryUser() {
		return entryUser;
	}
	public void setEntryUser(String entryUser) {
		this.entryUser = entryUser;
	}
	public CostFixService getCostFixService() {
		return costFixService;
	}
	public void setCostFixService(CostFixService costFixService) {
		this.costFixService = costFixService;
	}
	public DriverService getDriverService() {
		return driverService;
	}
	public void setDriverService(DriverService driverService) {
		this.driverService = driverService;
	}
	public TruckService getTruckService() {
		return truckService;
	}
	public void setTruckService(TruckService truckService) {
		this.truckService = truckService;
	}
	public InvoiceOrderService getInvoiceOrderService() {
		return invoiceOrderService;
	}
	public void setInvoiceOrderService(InvoiceOrderService invoiceOrderService) {
		this.invoiceOrderService = invoiceOrderService;
	}
	public List<CostFix> getCostFixs() {
		return costFixs;
	}
	public void setCostFixs(List<CostFix> costFixs) {
		this.costFixs = costFixs;
	}
	public List<TruckInfo> getTrucks() {
		return trucks;
	}
	public void setTrucks(List<TruckInfo> trucks) {
		this.trucks = trucks;
	}
	public List<InvoiceOrder> getInvoiceOrders() {
		return invoiceOrders;
	}
	public void setInvoiceOrders(List<InvoiceOrder> invoiceOrders) {
		this.invoiceOrders = invoiceOrders;
	}
	public List<DriverInfo> getDrivers() {
		return drivers;
	}
	public void setDrivers(List<DriverInfo> drivers) {
		this.drivers = drivers;
	}
	public String getDeleteIds() {
		return deleteIds;
	}
	public void setDeleteIds(String deleteIds) {
		this.deleteIds = deleteIds;
	}
	public Integer getPageCounts() {
		return pageCounts;
	}
	public void setPageCounts(Integer pageCounts) {
		this.pageCounts = pageCounts;
	}
	public String getCurrPageStr() {
		return currPageStr;
	}
	public void setCurrPageStr(String currPageStr) {
		this.currPageStr = currPageStr;
	}
	public Integer getTotalCounts() {
		return totalCounts;
	}
	public void setTotalCounts(Integer totalCounts) {
		this.totalCounts = totalCounts;
	}
}
